package robocopy.actions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import model.Parceiro;
import model.Usuario;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import functions.Server;





import view.CadastroUsuario;
import view.MainForm;



public class MainAction implements IWorkbenchWindowActionDelegate {
	private static Usuario user;
	public static Server server;
	
	public MainAction() {
	}
	
	public void run(IAction action) {
		verificaCadastro();
	}
	
	public void verificaCadastro(){
		XStream xs = new XStream(new DomDriver());
		xs.alias("Usuario",Usuario.class);
		xs.alias("Parceiro", Parceiro.class);
		File arq = new File(".usuario.xml");
		if (arq.exists()){
			try {							 
				FileReader reader = new FileReader(arq);
				Usuario user = (Usuario) xs.fromXML(reader);
				MainAction.user=user;
				MainForm main = new MainForm();
				main.run();						
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			CadastroUsuario.run();
		}		
	}

	public void selectionChanged(IAction action, ISelection selection) {
	}

	public void dispose() {
	}
	
	public void init(IWorkbenchWindow window) {

	}

	public static Usuario getUser() {
		return user;
	}

	public static void setUser(Usuario user) {
		MainAction.user = user;
	}
	
}